package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.TeachingProgressRequestDTO;
import com.tianya.domain.entity.Clazz;
import com.tianya.domain.entity.Teacher;
import com.tianya.domain.entity.Teachingprogress;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.TeachingProgressVO;
import com.tianya.mapper.*;
import com.tianya.service.TeachingprogressService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【teachingprogress(教学进度表)】的数据库操作Service实现
* @createDate 2025-05-24 19:00:11
*/
@Service
public class TeachingprogressServiceImpl extends ServiceImpl<TeachingprogressMapper, Teachingprogress>
    implements TeachingprogressService{
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private ClazzMapper clazzMapper;
    @Override
    public PageResultVO<TeachingProgressVO> listPage(Integer pageNum, Integer pageSize, TeachingProgressRequestDTO teachingProgressRequestDTO) {
        Page<Teachingprogress> pageParam = new Page<>(pageNum, pageSize);
        QueryWrapper<Teachingprogress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(teachingProgressRequestDTO.getCourseId()  != null,Teachingprogress::getCourseId, teachingProgressRequestDTO.getCourseId())
                .eq(teachingProgressRequestDTO.getClazzId()  != null,Teachingprogress::getClazzId, teachingProgressRequestDTO.getClazzId());

        Page<Teachingprogress> teachingprogressPage = this.page(pageParam, queryWrapper);
        List<TeachingProgressVO> teachingProgressVOList = new ArrayList<>();

        for (Teachingprogress teachingprogress : teachingprogressPage.getRecords()) {
            Long courseID = teachingprogress.getCourseId();
            String courseName = courseMapper.selectById(courseID).getName();
            Long teacherID = teachingprogress.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherID);
            Long userID = teacher.getUserId();
            User user = userMapper.selectById(userID);
            String teacherName = user!=null?user.getName():"unKnown";

            Long clazzID = teachingprogress.getClazzId();
            Clazz clazz = clazzMapper.selectById(clazzID);
            String clazzName = clazz!=null?clazz.getName():"unKnown";
            TeachingProgressVO teachingProgressVO = new TeachingProgressVO();
            teachingProgressVO.setClazzName(clazzName);
            BeanUtil.copyProperties(teachingprogress,teachingProgressVO);
            teachingProgressVO.setCourseName(courseName);
            teachingProgressVO.setTeacherName(teacherName);
            teachingProgressVO.setProgressId(teachingprogress.getProgressId());

            teachingProgressVOList.add(teachingProgressVO);
        }
        return new PageResultVO<>(teachingprogressPage.getTotal(),teachingProgressVOList);
    }
}




